﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Shop.Controllers
{
    public class ExperienceController : BaseController
    { 
        // GET: Shop/Experience
        public ActionResult Index(string code, string state, string openid)
        {
            AppSetting setting = new AppSetting();
            List<ProjectModel> list = new List<ProjectModel>();
            try
            {

            
            string access_token = "";
            string UserName = "";
            string headImage = "";
            if (string.IsNullOrEmpty(openid))
            {
                var i = GetOpenId(code);
                openid = i.openid;
                access_token = i.access_token;


                if (string.IsNullOrEmpty(openid))
                {
                    Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed78d8fdb0030e29&redirect_uri=http%3a%2f%2f{0}%2fshop%2fmain&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect", setting.AppDomainName.Replace("http://", "")));
                    return null;
                }

                var tt = GetUserInfo(openid, access_token);
                headImage = tt.headimgurl;
                UserName = tt.nickname;
                UtilNS.Util.WriteLog("UserName::" + UserName);
                UtilNS.Util.WriteLog("openid::" + openid);
            }

            using (var db = new qianjingEntities())
            {
                var customer = db.O_Customer.FirstOrDefault(a => a.ocOpenid == openid);
                    if (customer == null)
                    {
                        db.O_Customer.Add(new O_Customer
                        {
                            ocOpenid = openid,
                            ocNicName = UserName,
                            CreatedDate = DateTime.Now,
                            ocHeadImage = headImage,
                            RowStatus = 1,
                            ocGrades = 0,
                        });
                        db.SaveChanges();

                    }
                    else
                    {
                        customer.ocHeadImage = headImage;
                        customer.ocNicName = UserName;
                        db.SaveChanges();
                    }

            }
            ViewBag.OpenId = openid;
            ViewBag.State = state;
            ViewBag.Code = code;
            
            using (var db = new qianjingEntities())
            {
                var records = db.D_item.OrderBy(a=>a.DIMID).Where(a => a.RowStatus == 1&&a.dimType==1).ToList();
                foreach (var item in records)
                {
                    list.Add(new ProjectModel
                    {
                        dimDesc = item.dimDesc,
                        dimName=item.dimName,                  
                    });
                }
                var Good= db.O_Goods.OrderBy(a=>a.ogsArea).Where(a => a.RowStatus == 1 && a.ogsType == 1 &&a.CreatedBy== "meunItem").ToList();
                ViewBag.Good = Good;
            }


            ViewBag.OpenId = openid;
            }
            catch (Exception ex)
            {
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx719afdb0aa0ebec4&redirect_uri=http%3a%2f%2fwww.qianjing-china.com%2fthousandclear%2fshop%2fExperience&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
            }
            return View(list);
        }
        // POST: Shop/Experience
        [HttpPost]
        public ActionResult Index(string data)
        {
            AppSetting setting = new AppSetting();
            var post = JObject.Parse(data);
            string OGSID = post["OGSID"].Value<string>();
            string price = post["price"].Value<string>();
            string OpenId = post["OpenId"].Value<string>();
            string Number = DateTime.Now.ToString("yyMMddHHmmss");
            Random example = new Random();
            int Numradom = example.Next(10000, 99999);
            Number = Number + Numradom.ToString();
            using (var db = new qianjingEntities())
            {
                var toOCID = db.O_Customer.FirstOrDefault(a=>a.ocOpenid== OpenId).OCID;

                db.T_Order.Add(new T_Order
                {
                    toSerialNo= Number,
                    toOCID= Convert.ToInt16(toOCID),
                    toTotalRate = Convert.ToDecimal(price),
                    toTime=1,
                    toGoodNum=1,
                    toOGID=Convert.ToInt16(OGSID),
                    RowStatus = 0,
                    CreatedDate = DateTime.Now
                });
                db.SaveChanges();
            }
            RouteValueDictionary dicgt = new RouteValueDictionary();
            dicgt.Add("openid", OpenId);
            dicgt.Add("order", Number);
            return RedirectToAction("../Address/Index", dicgt);
        }

        //HttpGet:Shop/Experience
        [HttpGet]
        public int ChangArea(long id)
        {
            int totoalrate;
            using (var db = new qianjingEntities())
            {
                var query = db.O_Goods.FirstOrDefault(a => a.OGSID == id);
                totoalrate = Convert.ToInt16(query.ogsSalePrice);

            }
            return totoalrate;


        }

    }
}